//
//  HRVAverageView.swift
//  DaRings
//
//  Created by 易先生 on 2022/11/2.
//

import UIKit
import MYModule

class HRVAverageView: View {
    
    /// 当前 hrv
    public var hrv: Int? {
        
        didSet {
            
            guard let hrv = hrv else {
                hrvLabel.text = "--"
                return
            }

            hrvLabel.text = "\(hrv)"
        }
        
    }
    
   
    
    public var date: Date?
        
    public func slider(_ point: CGPoint) {
        
        let chartPoint = self.convert(point, to: chartView)
        
        chartView.slider(chartPoint)
        
    }
    
    public func showDefaultData() {
        
        if let hrv = self.hrv {
            self.hrvLabel.text = "\(hrv)"
        } else {
            self.hrvLabel.text = "--"
        }
        
        self.dateLabel.text = self.date?.localStringOfDateTime ?? "--"
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        bind()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func makeUI() {
        super.makeUI()
        
        self.addSubview(hrvStackView)
        self.addSubview(dateLabel)
        self.addSubview(chartView)
        
        hrvStackView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(30)
            make.centerX.equalToSuperview()
        }
        
        dateLabel.snp.makeConstraints { make in
            make.top.equalTo(hrvStackView.snp.bottom)
            make.centerX.equalToSuperview()
        }
        
        chartView.snp.makeConstraints { make in
            make.top.equalTo(dateLabel.snp.bottom).offset(20)
            make.leading.equalToSuperview()
            make.trailing.equalTo(-35)
            make.height.equalTo(124)
            make.bottom.equalToSuperview().offset(-20)
        }
        
    }
    
    
    func bind() {
        
        chartView.selectIndex.subscribe(onNext: {[weak self] index in
            
            guard let self = self, let index = index else {
        
                return
            }
            
           
            let minute: CGFloat = CGFloat(index * 30)
            let hrvData = self.chartView.hrvDatas?.filter({ $0.minute == minute }).first
            
            if let hrv = hrvData?.hrv {
                
                self.hrvLabel.text = "\(hrv)"
            } else {
                self.hrvLabel.text = "--"
            }
            
            if let date = self.date {
                let now = date.dateAtStartOf(.day) + Double(minute).minutes
                self.dateLabel.text = now.localStringOfDateTime
            } else {
                self.dateLabel.text = "--"
            }
            
            
        }).disposed(by: rx.disposeBag)
        
    }

    private lazy var hrvStackView: StackView = {
        let hrvStackView = StackView()
        hrvStackView.alignment = .firstBaseline
        hrvStackView.spacing = 2
        hrvStackView.axis = .horizontal
        hrvStackView.distribution = .fill
        hrvStackView.addArrangedSubview(hrvLabel)
        hrvStackView.addArrangedSubview(unitLabel)
        return hrvStackView
    }()
    
    public lazy var hrvLabel: Label = {
        let hrvLabel = Label()
        hrvLabel.text = "--"
        hrvLabel.textColor = UIColor.assist_222222
        hrvLabel.font = Fonts.boldSystemFont(ofSize: 44)
        return hrvLabel
    }()

    private lazy var unitLabel: Label = {
        let unitLabel = Label()
        unitLabel.text = "unit_hrv".language
        unitLabel.font = Fonts.systemFont(ofSize: 12)
        unitLabel.textColor = UIColor.assist_222222
        return unitLabel
    }()
    
    public lazy var dateLabel: Label = {
        let dateLabel = Label()
        dateLabel.font = Fonts.systemFont(ofSize: 12)
        dateLabel.textColor = UIColor.assist_222222
        return dateLabel
    }()
    
    public lazy var chartView: HRVChartView = {
        let chartView = HRVChartView()
        return chartView
    }()
    
//    private lazy var chartView: HeartRateChartView = {
//        let view = HeartRateChartView()
//        return view
//    }()

}
